<?php

namespace Admin\Controller;

class WorkorderController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("Workorder");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();

        $this->assign("begin_time", $_REQUEST["wo#create_time-egt"]);
        $this->assign("end_time", $_REQUEST["wo#create_time-elt"]);

        //用户
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
//        echo($user["employee_id"]);
//        $prescriber=D("Employee")->where("prescriber=$userId" and "employee_id=$userId")->find();

        //分页
        $map["status"] = array('neq','-1');
        $count = $this->currentModel->getListCount($requestMap, $map);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));
        //获取workorder列表
        $workorderList = $this->currentModel->queryForList($page, $requestMap, $map);
        $this->assign("statusEnum", C("WORKORDER_STATUS"));
        $this->assign("typeEnum", C("WORKORDER_TYPE"));
        $this->assign('user', $user);

        $this->assign('workorderList', $workorderList);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->assign("status", $_REQUEST["wo#status"]);
        $this->assign("type", $_REQUEST["wo#workorder_type"]);
        $this->display();

    }

    public function viewWorkorder() {
        $id = $_REQUEST["id"];
        $workorder = $this->currentModel->queryById($id);
        $patientInfo = D("Patient")->queryById($workorder["patient_id"]);

        $examAnalysis = D("ExamAnalysis")->queryById($workorder["exam_analysis_id"]);
        $examinator = D("Employee")->queryById($workorder["examinator_id"]);
        $creator = D("Employee")->queryById($workorder["creator_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $prescriber = D("Employee")->queryById($workorder["prescriber_id"]);
        if($workorder["workorder_type"] == 1){
            if($workorder["version"] == 1){
                $page1 = D("PrintTemplate")->queryById(1);
                $page2 = D("PrintTemplate")->queryById(2);
            }
            if($workorder["version"] == 2){
                $page1 = D("PrintTemplate")->queryById(111);
                $page2 = D("PrintTemplate")->queryById(222);
                $order1 = D("PrintTemplate")->queryById(8);
                $order2 = D("PrintTemplate")->queryById(9);
                $order3 = D("PrintTemplate")->queryById(10);
                $order4 = D("PrintTemplate")->queryById(11);
            }


            $examBasic = D("ExamBasic")->queryById($workorder["exam_basic_id"]);
            $mapEb['class_name'] = array('like', array('exam_basic', 'all'), 'or');
            $formDictEb = D("FormDict")->queryForList($mapEb);
            foreach ($formDictEb as $vo) {
                $formDictEb[$vo["index"]] = $vo["name"];
            }
            $gaitArr = explode(',', $examBasic["gait"]);
            $gait = "";
            foreach ($gaitArr as $vo) {
                if($gait == ""){
                    $gait = $formDictEb["gait_" . $vo];
                }else{
                    $gait = $gait . "," . $formDictEb["gait_" . $vo];
                }
            }
            if(!isNullOrEmpty($examBasic["notegait"])){
                $gait = $gait . "   备注:" . $examBasic["notegait"];
            }
            $examBasic["gait"] = $gait;

            $examBasicCP = D("ExamBasicCp")->queryById($workorder["exam_basic_cp_id"]);
            $examMeasure = D("ExamMeasure")->queryById($workorder["exam_measure_id"]);

            $rx = D("Rx")->queryById($workorder["rx_id"]);
            $map['class_name'] = array('like', array('rx', 'all'), 'or');
            $formDictRx = D("FormDict")->queryForList($map);
            foreach ($formDictRx as $vo) {
                $formDictRx[$vo["index"]] = $vo["name"];
            }

            $afotype_outArr = explode(',', $rx["afotype_out"]);
            $afotype_out = "";
            foreach ($afotype_outArr as $vo) {
                if($afotype_out == ""){
                    $afotype_out = $formDictRx["afotype_" . $vo];
                }else{
                    $afotype_out = $afotype_out . "," . $formDictRx["afotype_" . $vo];;
                }
            }

            $afotype_inArr = explode(',', $rx["afotype_in"]);
            $afotype_in = "";
            foreach ($afotype_inArr as $vo) {
                if($afotype_in == ""){
                    $afotype_in = $formDictRx["afotypein_" . $vo];
                }else{
                    $afotype_in = $afotype_in . "," . $formDictRx["afotypein_" . $vo];;
                }
            }

            $dorsal_supArr = explode(',', $rx["dorsal_sup"]);
            $dorsal_sup = "";
            foreach ($dorsal_supArr as $vo) {
                if($dorsal_sup == ""){
                    $dorsal_sup = $formDictRx["dorsal_sup_" . $vo];
                }else{
                    $dorsal_sup = $dorsal_sup . "," . $formDictRx["dorsal_sup_" . $vo];;
                }
            }

            $flexArr = explode(',', $rx["flex"]);
            $flex = "";
            foreach ($flexArr as $vo) {
                if($flex == ""){
                    $flex = $formDictRx["flex_" . $vo];
                }else{
                    $flex = $flex . "," . $formDictRx["flex_" . $vo];;
                }
            }

            $mould_outArr = explode(',', $rx["mould_out"]);
            $mould_out = "";
            foreach ($mould_outArr as $vo) {
                if($mould_out == ""){
                    $mould_out = $formDictRx["mould_" . $vo];
                }else{
                    $mould_out = $mould_out . "," . $formDictRx["mould_" . $vo];;
                }
            }

//            $mould_inArr = explode(',', $rx["mould_in"]);
//            $mould_in = "";
//            foreach ($mould_inArr as $vo) {
//                if($mould_in == ""){
//                    $mould_in = $formDictRx["mould_" . $vo];
//                }else{
//                    $mould_in = $mould_in . "," . $formDictRx["mould_" . $vo];;
//                }
//            }

            $cutline_outArr = explode(',', $rx["cutline_out"]);
            $cutline_out = "";
            foreach ($cutline_outArr as $vo) {
                if($cutline_out == ""){
                    $cutline_out = $formDictRx["cutline_" . $vo];
                }else{
                    $cutline_out = $cutline_out . "," . $formDictRx["cutline_" . $vo];;
                }
            }

            $cutline_inArr = explode(',', $rx["cutline_in"]);
            $cutline_in = "";
            foreach ($cutline_inArr as $vo) {
                if($cutline_in == ""){
                    $cutline_in = $formDictRx["cutlinein_" . $vo];
                }else{
                    $cutline_in = $cutline_in . "," . $formDictRx["cutlinein_" . $vo];;
                }
            }

            $pelma_outArr = explode(',', $rx["pelma_out"]);
            $pelma_out = "";
            foreach ($pelma_outArr as $vo) {
                if($pelma_out == ""){
                    $pelma_out = $formDictRx["pelma_" . $vo];
                }else{
                    $pelma_out = $pelma_out . "," . $formDictRx["pelma_" . $vo];;
                }
            }

            $pelma_inArr = explode(',', $rx["pelma_in"]);
            $pelma_in = "";
            foreach ($pelma_inArr as $vo) {
                if($pelma_in == ""){
                    $pelma_in = $formDictRx["pelma_" . $vo];
                }else{
                    $pelma_in = $pelma_in . "," . $formDictRx["pelma_" . $vo];;
                }
            }

            $this->assign("rx", $rx);
            $this->assign("afotype_out", $afotype_out);
            $this->assign("afotype_in", $afotype_in);
            $this->assign("dorsal_sup", $dorsal_sup);
            $this->assign("flex", $flex);
            $this->assign("mould_out", $mould_out);
//            $this->assign("mould_in", $mould_in);
            $this->assign("cutline_out", $cutline_out);
            $this->assign("cutline_in", $cutline_in);
            $this->assign("pelma_out", $pelma_out);
            $this->assign("pelma_in", $pelma_in);

        }
        if($workorder["workorder_type"] == 2){
            $page1 = D("PrintTemplate")->queryById(3);
            $page2 = D("PrintTemplate")->queryById(4);
            $examBasic = D("ExamBasicSpine")->queryById($workorder["exam_basic_spine_id"]);
            $examMeasure = D("ExamMeasureSpine")->queryById($workorder["exam_measure_spine_id"]);
        }
        if($workorder["workorder_type"] == 3){
            $page1 = D("PrintTemplate")->queryById(5);
            echo($workorder["exam_basic_cp_plus_id"]);
            $examBasic = D("ExamBasicCpPlus")->queryById($workorder["exam_basic_cp_id"]);
//            $examMeasure = D("ExamMeasureSpine")->queryById($workorder["exam_measure_spine_id"]);
        }

        $this->assign("workorder", $workorder);
        $this->assign("patientInfo", $patientInfo);
        $this->assign("examBasic", $examBasic);
        $this->assign("examBasicCP", $examBasicCP);
        $this->assign("examMeasure", $examMeasure);
        $this->assign("examAnalysis", $examAnalysis);
        $this->assign("examinator", $examinator);
        $this->assign("creator", $creator);
        $this->assign("sales", $sales);
        $this->assign("prescriber", $prescriber);
        $this->assign("page1", $page1);
        $this->assign("page2", $page2);
        $this->assign("order1", $order1);
        $this->assign("order2", $order2);
        $this->assign("order3", $order3);
        $this->assign("order4", $order4);

        $this->display();

    }

    public function orderTaken()
    {

        $id = $_REQUEST["id"];

        $workorder = $this->currentModel->queryById($id);
        $workorder["status"] = C("WORKORDER_STATUS.PROCESSING");
        $workorder["processing_id"] = $_SESSION['ADMIN_ID'];
        $workorder["processing_date"] = getCurrentDate();

        //推送留言信息到销售
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',',$singleEmployee["job"]);
            if($job[1] == "2"){
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if($singleEmployee["employee_id"] == $workorder["examinator_id"] && !in_array($singleEmployee["openid"],$openidArray)){
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"],$openidArray)){
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }

        foreach ($openidArray as $openid) {
            $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["openid"] = $openid;
            $notifyData["first"] = "DRAFO工单已经被接单：" . $user["admin_name"];
            $notifyData["keyword1"] = $workorder["workorder_no"];
            $notifyData["keyword2"] = "[诊断--".$patient["diagnosis"]."]";
            $notifyData["keyword3"] = $workorder["processing_date"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"];
            WorkorderProcessingNotify($notifyData);
        }
        $this->currentModel->updateInfo($workorder);
        $this->success("接单成功！工单号:".$id, U("workorder/index"));

    }

    public function endWorkorder() {

        $id = $_REQUEST["id"];
        $workorder = $this->currentModel->queryById($id);
        $workorder["status"] = C("WORKORDER_STATUS.END_PROCESS");
        $workorder["delivery_id"] = $_SESSION['ADMIN_ID'];
        $workorder["delivery_date"] = getCurrentDate();

        $this->currentModel->updateInfo($workorder);

        //推送留言信息到销售
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $sales = D("Employee")->queryById($workorder["sales_id"]);
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();

        $employeeList = D("Employee")->alias("e")
            ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
            ->field("e.*, c.openid")
            ->select();
        $openidArray = array();
        foreach ($employeeList as $singleEmployee) {
            $job = explode(',',$singleEmployee["job"]);
            if($job[1] == "2"){
                array_push($openidArray, $singleEmployee["openid"]);
            }
//            if($job[4] == "1" && !in_array($singleEmployee["openid"],$openidArray)){
//                array_push($openidArray, $singleEmployee["openid"]);
//            }
            if($singleEmployee["employee_id"] == $workorder["examinator_id"] && !in_array($singleEmployee["openid"],$openidArray)){
                array_push($openidArray, $singleEmployee["openid"]);
            }
            if($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"],$openidArray)){
                array_push($openidArray, $singleEmployee["openid"]);
            }
        }

        foreach ($openidArray as $openid) {
            $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
            $notifyData["openid"] = $openid;
            $notifyData["first"] = "DRAFO工单已交付。";
            $notifyData["keyword1"] = "工单号" . $workorder["workorder_no"];
            $notifyData["keyword2"] = "亦庄临床部";
            $notifyData["keyword3"] = $workorder["processing_date"];
            $notifyData["keyword4"] = $workorder["delivery_date"];
            $notifyData["keyword5"] = $user["admin_name"];
            $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"];
            workorderEndNotify($notifyData);
        }
        $this->success("已标记订单为已交付状态", U("Workorder/index"));

    }

    public function rx() {
        $rxId = $_REQUEST["rxId"];
        $id = $_REQUEST["id"];
        $hisId = $_REQUEST["hisId"];

        $workorder = $this->currentModel->queryById($id);

        if (isNullOrEmpty($rxId)) {
            $rxId = $workorder["rx_id"];
        }
        if (!isNullOrEmpty($hisId)) {
            $rx = D("Rx")->queryById($hisId);
            $rx["rx_id"] = $rxId;
        }else{
            $rx = D("Rx")->queryById($rxId);
        }

        $map['class_name'] = array('like', array('rx', 'all'), 'or');
        $formDict = D("FormDict")->queryForList($map);

//        $patientMap["patient_name"] = $patient["patient_name"];
//        $map["id_no"] = I("post.idNo");
//        $rxList = D("Rx")->queryForList(null,$patientMap);
        $patient = D("Patient")->queryById($workorder["patient_id"]);
        $workorder["patient_name"] = $patient["patient_name"];
        $hisRx = null;
        if(isNullOrEmpty($workorder["rx_id"]) && isNullOrEmpty($hisId)){
            $patient = D("Patient")->queryById($workorder["patient_id"]);
            $patientMap["patient_name"] = $patient["patient_name"];
            $patientMap["id_no"] = $patient["id_no"];

            $rxList = D('Rx')->alias('r')
                ->join("left join __ADMIN__ a on a.user_id = r.update_id")
                ->where($patientMap)
                ->order("r.rx_id desc")
                ->field("r.*, a.admin_name")
                ->select();
            if(!isNullOrEmpty($rxList)){
                $hisRx = $rxList[0];
            }
        }

        $this->assign("rx", $rx);
        $this->assign("rxId", $rxId);
        $this->assign("hisRx", $hisRx);
        $this->assign("id", $id);
        $this->assign("workorder", $workorder);
        $this->assign("formDict", $formDict);
        $this->display();

    }

    function rx_post()
    {
        if (IS_POST) {

            $rxId = $_REQUEST["rxId"];
            $id = $_REQUEST["id"];
            $workorder = $this->currentModel->queryById($id);
//            if (isNullOrEmpty($rxId)) {
//                $rxId = $workorder["rx_id"];
//            }
            $patient = D("Patient")->queryById($workorder["patient_id"]);
            if (isNullOrEmpty($rxId)) {
                $rxId = D("Rx")->add(array(
                    'completed' => -1,
                    'version' => 2,
                    'create_time' => getCurrentDate(),
                    'update_time' => getCurrentDate(),
                    'update_id' => $_SESSION['ADMIN_ID'],
                    'patient_name' => $patient["patient_name"],
                    'id_no' => $patient["id_no"],
                    'side' => $workorder["side"],
                ));
                $workorder["rx_id"] = $rxId;
                D("Workorder")->updateInfo($workorder);
            }
            $rx = D("Rx")->queryById($rxId);

            $rx["combo"] = I('post.combo');

            $afotype_out_str = "";
            $afotype_out_ids = I('post.afotype_out_id');
            foreach ($afotype_out_ids as $afotype_id) {
                if($afotype_out_str == ""){
                    $afotype_out_str = $afotype_id;
                }else{
                    $afotype_out_str = $afotype_out_str . "," . $afotype_id;
                }
            }
            $rx["afotype_out"] = $afotype_out_str;

   //         if($rx["combo"] == "是"){
                $afotype_in_str = "";
                $afotype_in_ids = I('post.afotype_in_id');
                foreach ($afotype_in_ids as $afotype_id) {
                    if($afotype_in_str == ""){
                        $afotype_in_str = $afotype_id;
                    }else{
                        $afotype_in_str = $afotype_in_str . "," . $afotype_id;
                    }
                }
                $rx["afotype_in"] = $afotype_in_str;
                $rx["afotype_in_note"] = I('post.afotype_in_note');
  //          }

            $rx["afotype_out_note"] = I('post.afotype_out_note');
            $rx["afotype_other_note"] = I('post.afotype_other_note');
            $rx["ahtype"] = I('post.ahtype');
            $rx["ahsize"] = I('post.ahsize');


//            $dorsal_sup_str = "";
//            $dorsal_sup_ids = I('post.dorsal_sup_id');
//            foreach ($dorsal_sup_ids as $dorsal_sup_id) {
//                if($dorsal_sup_str == ""){
//                    $dorsal_sup_str = $dorsal_sup_id;
//                }else{
//                    $dorsal_sup_str = $dorsal_sup_str . "," . $dorsal_sup_id;
//                }
//            }
//            $rx["dorsal_sup"] = $dorsal_sup_str;

            $rx["activity_limit"] = I('post.activity_limit');
            $rx["limit_level"] = I('post.limit_level');
            $rx["limit_level_angle"] = I('post.limit_level_angle');
            $rx["limit_level_other"] = I('post.limit_level_other');

            $flex_str = "";
            $flex_ids = I('post.flex_id');
            foreach ($flex_ids as $flex_id) {
                if($flex_str == ""){
                    $flex_str = $flex_id;
                }else{
                    $flex_str = $flex_str . "," . $flex_id;
                }
            }

            $rx["flex"] = $flex_str;
            $rx["flex_other"] = I('post.flex_other');
            $rx["sag_aa"] = I('post.sag_aa');
            $rx["sag_sva"] = I('post.sag_sva');
            $rx["heel"] = I('post.heel');
            $rx["heel_angle"] = I('post.heel_angle');
            $rx["forefoot"] = I('post.forefoot');
            $rx["outproduct"] = I('post.outproduct');
            $rx["out_pp"] = I('post.out_pp');
            $rx["out_carbpp"] = I('post.out_carbpp');
            $rx["out_copp"] = I('post.out_copp');
            $rx["out_pe"] = I('post.out_pe');
            $rx["out_enhpe"] = I('post.out_enhpe');
            $rx["out_dy"] = I('post.out_dy');

    //        if($rx["combo"] == "是") {
                $rx["inproduct"] = I('post.inproduct');
                $rx["in_dy"] = I('post.in_dy');
                $rx["in_enhpe"] = I('post.in_enhpe');
                $rx["in_copp"] = I('post.in_copp');
                $rx["in_pp"] = I('post.in_pp');
                $rx["in_eva"] = I('post.in_eva');
                $rx["innerproduct"] = I('post.innerproduct');
                $rx["inner_al"] = I('post.inner_al');
                $rx["inner_eva"] = I('post.inner_eva');
                $rx["inner_other"] = I('post.inner_other');
       //     }

            $mould_out_str = "";
            $mould_out_ids = I('post.mould_out_id');
            foreach ($mould_out_ids as $mould_out_id) {
                if($mould_out_str == ""){
                    $mould_out_str = $mould_out_id;
                }else{
                    $mould_out_str = $mould_out_str . "," . $mould_out_id;
                }
            }
            $rx["mould_out"] = $mould_out_str;

            $rx["mould_out_a"] = I('post.mould_out_a');
            $rx["mould_out_b"] = I('post.mould_out_b');
            $rx["mould_out_c"] = I('post.mould_out_c');
            $rx["mould_out_d"] = I('post.mould_out_d');
            $rx["mould_out_e"] = I('post.mould_out_e');
            $rx["mould_out_f"] = I('post.mould_out_f');
            $rx["mould_out_g"] = I('post.mould_out_g');
            $rx["mould_out_h"] = I('post.mould_out_h');
            $rx["mould_out_i"] = I('post.mould_out_i');

//            if($rx["combo"] == "是") {
//                $mould_in_str = "";
//                $mould_in_ids = I('post.mould_in_id');
//                foreach ($mould_in_ids as $mould_in_id) {
//                    if ($mould_in_str == "") {
//                        $mould_in_str = $mould_in_id;
//                    } else {
//                        $mould_in_str = $mould_in_str . "," . $mould_in_id;
//                    }
//                }
//                $rx["mould_in"] = $mould_in_str;
//
//                $rx["mould_in_a"] = I('post.mould_in_a');
//                $rx["mould_in_b"] = I('post.mould_in_b');
//                $rx["mould_in_c"] = I('post.mould_in_c');
//                $rx["mould_in_d"] = I('post.mould_in_d');
//                $rx["mould_in_e"] = I('post.mould_in_e');
//            }

            $rx["wedge_out_pos"] = I('post.wedge_out_pos');
            $rx["wedge_out_a"] = I('post.wedge_out_a');
            $rx["wedge_out_b"] = I('post.wedge_out_b');
            $rx["wedge_out_c"] = I('post.wedge_out_c');
            $rx["wedge_out_d"] = I('post.wedge_out_d');
            $rx["wedge_out_e"] = I('post.wedge_out_e');
            $rx["wedge_out_f"] = I('post.wedge_out_f');
            $rx["wedge_out_g"] = I('post.wedge_out_g');
    //        if($rx["combo"] == "是") {
                $rx["wedge_in_pos"] = I('post.wedge_in_pos');
                $rx["wedge_in_a"] = I('post.wedge_in_a');
                $rx["wedge_in_b"] = I('post.wedge_in_b');
                $rx["wedge_in_c"] = I('post.wedge_in_c');
                $rx["wedge_in_d"] = I('post.wedge_in_d');
                $rx["wedge_in_e"] = I('post.wedge_in_e');
                $rx["wedge_in_f"] = I('post.wedge_in_f');
                $rx["wedge_in_g"] = I('post.wedge_in_g');
    //        }

            $rx["sole_a"] = I('post.sole_a');
            $rx["sole_b"] = I('post.sole_b');
            $rx["sole_b_angle"] = I('post.sole_b_angle');
            $rx["sole_c"] = I('post.sole_c');
            $rx["sole_d"] = I('post.sole_d');
            $rx["hasp_out_len_a"] = I('post.hasp_out_len_a');
            $rx["hasp_out_type_a"] = I('post.hasp_out_type_a');
            $rx["hasp_out_start_a"] = I('post.hasp_out_start_a');
            $rx["hasp_out_side_a"] = I('post.hasp_out_side_a');
            $rx["hasp_out_start_a1"] = I('post.hasp_out_start_a1');
            $rx["hasp_out_side_a1"] = I('post.hasp_out_side_a1');
            $rx["hasp_out_tight_a"] = I('post.hasp_out_tight_a');
            $rx["hasp_out_other_a"] = I('post.hasp_out_other_a');
            $rx["hasp_out_len_b"] = I('post.hasp_out_len_b');
            $rx["hasp_out_type_b"] = I('post.hasp_out_type_b');
            $rx["hasp_out_start_b"] = I('post.hasp_out_start_b');
            $rx["hasp_out_side_b"] = I('post.hasp_out_side_b');
            $rx["hasp_out_start_b1"] = I('post.hasp_out_start_b1');
            $rx["hasp_out_side_b1"] = I('post.hasp_out_side_b1');
            $rx["hasp_out_tight_b"] = I('post.hasp_out_tight_b');
            $rx["hasp_out_other_b"] = I('post.hasp_out_other_b');
            $rx["hasp_out_len_c"] = I('post.hasp_out_len_c');
            $rx["hasp_out_type_c"] = I('post.hasp_out_type_c');
            $rx["hasp_out_start_c"] = I('post.hasp_out_start_c');
            $rx["hasp_out_side_c"] = I('post.hasp_out_side_c');
            $rx["hasp_out_start_c1"] = I('post.hasp_out_start_c1');
            $rx["hasp_out_side_c1"] = I('post.hasp_out_side_c1');
            $rx["hasp_out_cut_c"] = I('post.hasp_out_cut_c');
            $rx["hasp_out_tight_c"] = I('post.hasp_out_tight_c');
            $rx["hasp_out_other_c"] = I('post.hasp_out_other_c');
            $rx["hasp_out_len_d"] = I('post.hasp_out_len_d');
            $rx["hasp_out_type_d"] = I('post.hasp_out_type_d');
            $rx["hasp_out_start_d"] = I('post.hasp_out_start_d');
            $rx["hasp_out_side_d"] = I('post.hasp_out_side_d');
            $rx["hasp_out_start_d1"] = I('post.hasp_out_start_d1');
            $rx["hasp_out_side_d1"] = I('post.hasp_out_side_d1');
            $rx["hasp_out_tight_d"] = I('post.hasp_out_tight_d');
            $rx["hasp_out_other_d"] = I('post.hasp_out_other_d');

    //        if($rx["combo"] == "是") {
                $rx["hasp_in_len_a"] = I('post.hasp_in_len_a');
                $rx["hasp_in_type_a"] = I('post.hasp_in_type_a');
                $rx["hasp_in_start_a"] = I('post.hasp_in_start_a');
                $rx["hasp_in_side_a"] = I('post.hasp_in_side_a');
                $rx["hasp_in_start_a1"] = I('post.hasp_in_start_a1');
                $rx["hasp_in_side_a1"] = I('post.hasp_in_side_a1');
                $rx["hasp_in_tight_a"] = I('post.hasp_in_tight_a');
                $rx["hasp_in_other_a"] = I('post.hasp_in_other_a');
                $rx["hasp_in_len_b"] = I('post.hasp_in_len_b');
                $rx["hasp_in_type_b"] = I('post.hasp_in_type_b');
                $rx["hasp_in_start_b"] = I('post.hasp_in_start_b');
                $rx["hasp_in_side_b"] = I('post.hasp_in_side_b');
                $rx["hasp_in_start_b1"] = I('post.hasp_in_start_b1');
                $rx["hasp_in_side_b1"] = I('post.hasp_in_side_b1');
                $rx["hasp_in_tight_b"] = I('post.hasp_in_tight_b');
                $rx["hasp_in_other_b"] = I('post.hasp_in_other_b');
                $rx["hasp_in_len_c"] = I('post.hasp_in_len_c');
                $rx["hasp_in_type_c"] = I('post.hasp_in_type_c');
                $rx["hasp_in_start_c"] = I('post.hasp_in_start_c');
                $rx["hasp_in_side_c"] = I('post.hasp_in_side_c');
                $rx["hasp_in_start_c1"] = I('post.hasp_in_start_c1');
                $rx["hasp_in_side_c1"] = I('post.hasp_in_side_c1');
                $rx["hasp_in_cut_c"] = I('post.hasp_in_cut_c');
                $rx["hasp_in_tight_c"] = I('post.hasp_in_tight_c');
                $rx["hasp_in_other_c"] = I('post.hasp_in_other_c');
//                $rx["hasp_in_len_d"] = I('post.hasp_in_len_d');
//                $rx["hasp_in_type_d"] = I('post.hasp_in_type_d');
//                $rx["hasp_in_side_d"] = I('post.hasp_in_side_d');
//                $rx["hasp_in_tight_d"] = I('post.hasp_in_tight_d');
//                $rx["hasp_in_other_d"] = I('post.hasp_in_other_d');
  //          }

            $cutline_out_str = "";
            $cutline_out_ids = I('post.cutline_out_id');
            foreach ($cutline_out_ids as $cutline_out_id) {
                if($cutline_out_str == ""){
                    $cutline_out_str = $cutline_out_id;
                }else{
                    $cutline_out_str = $cutline_out_str . "," . $cutline_out_id;
                }
            }
            $rx["cutline_out"] = $cutline_out_str;
            $rx["cutline_out_other"] = I('post.cutline_out_other');
            $cutline_in_str = "";
            $cutline_in_ids = I('post.cutline_in_id');
            foreach ($cutline_in_ids as $cutline_in_id) {
                if($cutline_in_str == ""){
                    $cutline_in_str = $cutline_in_id;
                }else{
                    $cutline_in_str = $cutline_in_str . "," . $cutline_in_id;
                }
            }

     //       if($rx["combo"] == "是") {
                $rx["cutline_in"] = $cutline_in_str;
                $rx["cutline_in_other"] = I('post.cutline_in_other');
   //         }

            $pelma_out_str = "";
            $pelma_out_ids = I('post.pelma_out_id');
            foreach ($pelma_out_ids as $pelma_out_id) {
                if($pelma_out_str == ""){
                    $pelma_out_str = $pelma_out_id;
                }else{
                    $pelma_out_str = $pelma_out_str . "," . $pelma_out_id;
                }
            }
            $rx["pelma_out"] = $pelma_out_str;
            $rx["pelma_out_other"] = I('post.pelma_out_other');

     //       if($rx["combo"] == "是") {
                $pelma_in_str = "";
                $pelma_in_ids = I('post.pelma_in_id');
                foreach ($pelma_in_ids as $pelma_in_id) {
                    if ($pelma_in_str == "") {
                        $pelma_in_str = $pelma_in_id;
                    } else {
                        $pelma_in_str = $pelma_in_str . "," . $pelma_in_id;
                    }
                }
                $rx["pelma_in"] = $pelma_in_str;
                $rx["pelma_in_other"] = I('post.pelma_in_other');
     //       }

            $rx["note"] = I('post.note');
            $rx["price"] = I('post.price');
            $rx["other_type"] = I('post.other_type');
            $rx["other_price"] = I('post.other_price');
            $rx["delivery_date"] = I("post.delivery_date");

            $rx["update_time"] = getCurrentDate();
            $rx["update_id"] = $_SESSION['ADMIN_ID'];

            if(I('post.completed') == "on"){
                $rx["completed"] = 1;
                $workorder["status"] = 2;
                $workorder["prescribe_time"] = getCurrentDate();
                $workorder["price"] = I('post.price');
                $workorder["delivery_date"] = I("post.delivery_date");
                D("Workorder")->updateInfo($workorder);

                //推送留言信息到销售
                $patient = D("Patient")->queryById($workorder["patient_id"]);
                $sales = D("Employee")->queryById($workorder["sales_id"]);
                $prescriber = D("Employee")->queryById($workorder["prescriber_id"]);
                $users_obj= M("admin");
                $userId=$_SESSION['ADMIN_ID'];
                $user=$users_obj->where("user_id=$userId")->find();
                $employeeList = D("Employee")->alias("e")
                    ->join("left join __CUSTOMER__ c on c.customer_id = e.employee_id")
                    ->field("e.*, c.openid")
                    ->select();
                $openidArray = array();
                foreach ($employeeList as $singleEmployee) {
                    $job = explode(',',$singleEmployee["job"]);
                    if($job[0] == "4" || $job[1] == "2" || $job[0] == "100"){
                        array_push($openidArray, $singleEmployee["openid"]);
                    }
                    if($singleEmployee["employee_id"] == $workorder["examinator_id"] && !in_array($singleEmployee["openid"],$openidArray)){
                        array_push($openidArray, $singleEmployee["openid"]);
                    }
                    if($singleEmployee["employee_id"] == $workorder["sales_id"] && !in_array($singleEmployee["openid"],$openidArray)){
                        array_push($openidArray, $singleEmployee["openid"]);
                    }
                    if ($singleEmployee["employee_id"] == $workorder["prescriber_id"] && !in_array($singleEmployee["openid"], $openidArray)) {
                        array_push($openidArray, $singleEmployee["openid"]);
                    }
                }

                foreach ($openidArray as $openid) {
                    $notifyData["url"] = "http://xwkang.com/wechat/workorder/index/id/" . $workorder["workorder_id"];
                    $notifyData["openid"] = $openid;
                    $notifyData["first"] = "DRAFO工单已经由：" . $user["admin_name"]."开处方，请临床部协调人校验工单；请临床部领料人申请领料。";
                    $notifyData["keyword1"] = $workorder["workorder_no"];
                    $notifyData["keyword2"] = "[诊断--".$patient["diagnosis"]."]";
                    $notifyData["keyword3"] = $workorder["processing_date"];
                    $notifyData["remark"] = "患者基本信息：" . $patient["patient_name"] . "，" . $patient["sex"] . "，" . $patient["age"] . "岁。 销售：" . $sales["employee_name"]. "。处方人：" . $user["admin_name"];
                    WorkorderProcessingNotify($notifyData);
                }
            }

            D("Rx")->updateInfo($rx);
            $this->success("保存成功！", U("Workorder/index"));
        } else {
            $this->error("请为此用户指定角色！");
        }
    }

}

